<?php defined('SYSPATH') or die('No direct access allowed.');


class Model_Desktop_Item extends ORM {

        // Relationships
        protected $_belongs_to = array('desktop' => array('model' => 'user_desktop', 'foreign_key' => 'desktop_id'),
                                       'icon' => array('model' => 'file', 'foreign_key' => 'icon_id'),
                                       'thumbnail' => array('model' => 'file', 'foreign_key' => 'thumbnail_id'));
        
        protected $_has_one =  array('project' => array('model' => 'desktop_project', 'foreign_key' => 'item_id'),
                                     'link'    => array('model' => 'desktop_link', 'foreign_key' => 'item_id')); 
        
        // Rules
        public function rules()
	{
		return array(
			'title' => array(
				array('not_empty'),
				array('max_length', array(':value', 127)),
			),
                        'description' => array(
				array('not_empty'),
				array('max_length', array(':value', 254)),                       
			)         
		);
	}
        
        public function filters()
	{
		return array(
			'title' => array(
                                //xss clean
				array(array('Security', 'xss_clean')),
                                array('trim')
			),
                        'description' => array(
				array(array('Security', 'xss_clean')),
                                array('trim')
			)
                       
		);
	}

	/**
	 * Labels for fields in this model
	 *
	 * @return array Labels
	 */
	public function labels()
	{
		return array(
			'title'         => __('Title'),
			'description'   => __('Description')
		);
	}
        
        public function get_items($desktop_id)
        {
           $items_temp = DB::select(array('desktop_items.id','item_id'),
                                    array('desktop_items.title','item_title'),
                                    array('desktop_items.description','item_description'),
                                    array('desktop_items.pos_x','item_pos_x'),
                                    array('desktop_items.pos_y','item_pos_y'),
                                    array('desktop_items.icon_id','icon'),
                                    array('desktop_items.thumbnail_id','thumbnail'),
                                    array('desktop_items.date_modify','published'),
                                    array('desktop_panels.id','panel_id'),
                                    array('desktop_panels.title','panel_title'),
                                    array('desktop_panels.pos_x','panel_pos_x'),
                                    array('desktop_panels.pos_y','panel_pos_y'),
                                    array('desktop_panels.width','panel_width'),
                                    array('desktop_panels.height','panel_height'),
                                    array('desktop_panels.rows','panel_rows'),
                                    array('desktop_panels.columns','panel_columns'),
                                    array('desktop_projects.id','project_id'),
                                    array('fields.name', 'project_field'),
                                    array('desktop_links.link','link_url'),
                                    array('desktop_links.embed','link_embed'),
                                    array('desktop_links.id','link_id'),
                                    array('file_icons.id','file_id'),
                                    array('file_icons.name','file_name'),
                                    array('file_icons.extension','file_extension'),
                                    array('file_thumbnails.id','file_thumbnail_id'),
                                    array('file_thumbnails.name','file_thumbnail_name'),
                                    array('file_thumbnails.extension','file_thumbnail_extension'))
                              ->from('desktop_items')
                              ->join('desktop_panels','LEFT')->on('desktop_items.panel_id', '=', 'desktop_panels.id')
                              ->join('desktop_projects', 'LEFT')->on('desktop_items.id', '=', 'desktop_projects.item_id')
                              ->join('fields','LEFT')->on('fields.id', '=', 'desktop_projects.field_id')
                              ->join('desktop_links', 'LEFT')->on('desktop_items.id', '=', 'desktop_links.item_id')
                              ->join(array('files','file_icons'),'LEFT')->on('desktop_items.icon_id', '=', 'file_icons.id')
                              ->join(array('files','file_thumbnails'),'LEFT')->on( 'desktop_items.thumbnail_id', '=', 'file_thumbnails.id')
                              ->where('desktop_items.desktop_id', '=', (int)$desktop_id)
                              ->order_by('item_pos_y', 'ASC')
                              ->order_by('item_pos_x', 'ASC')
                              ->as_assoc()->execute();
           
           
        
           //get icon address
           $items = array();
           foreach($items_temp as $item)
           {       
              $item['icon'] = Media::get_file_path($item['file_name'], 
                                                   $item['file_id'], 
                                                   $item['file_extension'], 
                                                   false);
              
              $item['thumbnail'] = Media::get_file_path($item['file_thumbnail_name'], 
                                                        $item['file_thumbnail_id'], 
                                                        $item['file_thumbnail_extension'], 
                                                        true);
              
              $item['project_field'] = __($item['project_field']);
              
              $item['item_description'] = Text::limit_chars($item['item_description'], 100, '...', true);
              
              $item['published'] = (($item['project_id'] !== null AND $item['published']) OR $item['project_id'] === null);
              
              $item_arr = $item;
              $item_arr['type'] = $item_arr['project_id'] == null ? 'link' : 'project';  
              
              if($item_arr['type'] == 'link')
              {
                $item_arr['link'] = $item['link_url'];
                $item_arr['embed'] = (boolean)$item['link_embed'];
              }     

              $items[] = $item_arr;
            }
            
            return $items;   
        }
        
        public function get_item($id, $type)
        {
            
            $item_temp = array();
            
            if($type === 'project')
            {
                $item_temp = DB::select(array('desktop_items.id','item_id'),
                                        array('desktop_items.title','item_title'),
                                        array('desktop_items.description','item_description'),
                                        array('file_icons.id','icon'),
                                        array('file_icons.id','file_id'),
                                        array('file_icons.name','file_name'),
                                        array('file_icons.extension','file_extension'),
                                        array('desktop_projects.field_id','field_id'),
                                        array('desktop_projects.id','project_id'),
                                        array('user_desktops.user_id','user_id'))
                                ->from('desktop_items')
                                ->join(array('files','file_icons'),'LEFT')->on('desktop_items.icon_id', '=', 'file_icons.id')
                                ->join('user_desktops')->on('user_desktops.id', '=', 'desktop_items.desktop_id')
                                ->join('desktop_projects')->on('desktop_projects.item_id', '=', 'desktop_items.id')
                                ->where('desktop_items.id','=',(int) $id)
                                ->as_assoc()->execute();
                
            }
            elseif($type === 'link')
            {
                $item_temp = DB::select(array('desktop_items.id','item_id'),
                                         array('desktop_items.title','item_title'),
                                         array('desktop_items.description','item_description'),
                                         array('file_icons.id','icon'),
                                         array('file_icons.id','file_id'),
                                         array('file_icons.name','file_name'),
                                         array('file_icons.extension','file_extension'),
                                         array('desktop_links.link','link'),
                                         array('desktop_links.id','link_id'),
                                         array('user_desktops.user_id','user_id'))
                                ->from('desktop_items')
                                ->join(array('files','file_icons'),'LEFT')->on('desktop_items.icon_id', '=', 'file_icons.id')
                                ->join('user_desktops')->on('user_desktops.id', '=', 'desktop_items.desktop_id')
                                ->join('desktop_links')->on('desktop_links.item_id', '=', 'desktop_items.id')
                                ->where('desktop_items.id','=',(int) $id)
                                ->as_assoc()->execute();
            }

            if($item_temp)
            {
                $item = $item_temp[0];
                $item['icon']= Media::get_file_path($item['file_name'], 
                                                     $item['file_id'], 
                                                     $item['file_extension'], 
                                                     false);

                return $item;
            }
            else
                return false;
        }
        
}
